﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [ToolboxData("<{0}:RadGridRadioButtonListColumn runat=server></{0}:RadGridRadioButtonListColumn>")]
    public class RadGridRadioButtonListColumn : BaseRadGridColumn
    {
        public string DataValueField { get; set; }
        public string DataSourceID { get; set; }
        public string RadioButtonValueField { get; set; }
        public string RadioButtonValueFormat { get; set; }
        public string RadioButtonDisplayField { get; set; }
        public string RadioButtonDisplayFormat { get; set; }
        public string RadioButtonTextField { get; set; }
        public string RadioButtonTextFormat { get; set; }
        public string RadioButtonCssClassField { get; set; }
        public string RadioButtonCssClassFormat { get; set; }
        public string RadioButtonControlCssClassField { get; set; }
        public string RadioButtonControlCssClassFormat { get; set; }
        public string RadioButtonLabelCssClassField { get; set; }
        public string RadioButtonLabelCssClassFormat { get; set; }
        public Acms.WebControls.AcmsRadioButton.LabelPositions LabelPosition { get; set; }

        public override Telerik.Web.UI.GridColumn Clone()
        {
            return new RadGridRadioButtonColumn();
        }
        
        protected override string GetControlIdToValidate(Telerik.Web.UI.GridItem item)
        {
            return string.Format("rbl_{0}_{1}", this.UniqueName, item.ItemIndex);
        }

        protected override void InitalizeEditCell(System.Web.UI.WebControls.TableCell cell, int columnIndex, Telerik.Web.UI.GridItem item)
        {
            AcmsRadioButtonList ctrl = new AcmsRadioButtonList();
            ctrl.ID = GetControlIdToValidate(item);
            ctrl.DataBound += new EventHandler(ctrl_DataBound);
            ctrl.Init += new EventHandler(ctrl_Init);
            ctrl.Load += new EventHandler(ctrl_Load);
            ctrl.DataSourceID = DataSourceID;
            ctrl.DataCssClassField = RadioButtonCssClassField;
            ctrl.DataCssClassFormat = RadioButtonCssClassFormat;
            ctrl.DataLabelCssClassField = RadioButtonLabelCssClassField;
            ctrl.DataLabelCssClassFormat = RadioButtonLabelCssClassFormat;
            ctrl.DataRadioButtonCssClassField = RadioButtonControlCssClassField;
            ctrl.DataRadioButtonCssClassFormat = RadioButtonControlCssClassFormat;
            ctrl.DataTextField = RadioButtonDisplayField;
            ctrl.DataTextFormat = RadioButtonDisplayFormat;
            ctrl.DataValueField = RadioButtonValueField;
            ctrl.DataValueFormat = RadioButtonValueFormat;
            ctrl.LabelPosition = LabelPosition;
            cell.Controls.Add(ctrl);
        }

        void ctrl_Load(object sender, EventArgs e)
        {
            if (((AcmsRadioButtonList)sender).SelectedValue == "")
            {
            }
        }

        void ctrl_Init(object sender, EventArgs e)
        {
            if (((AcmsRadioButtonList)sender).SelectedValue == "")
            {
            }
        }

        void ctrl_DataBound(object sender, EventArgs e)
        {
            AcmsRadioButtonList ctrl = sender as AcmsRadioButtonList;
            object item = GetDataItem(ctrl.NamingContainer);
            if (item != null)
            {
                object val = DataBinder.Eval(item, DataValueField);
                if (val != null)
                {
                    ctrl.SelectedValue = val.ToString();
                }
            }
        }

        public override void FillValues(System.Collections.IDictionary newValues, Telerik.Web.UI.GridEditableItem editableItem)
        {
            AcmsRadioButtonList ctrl = editableItem.FindControl(GetControlIdToValidate(editableItem)) as AcmsRadioButtonList;
            if (ctrl != null)
            {
                newValues.Add(DataValueField, ctrl.SelectedValue);
            }
        }
    }
}
